package marksys.free.com.marksys.utils;

import android.content.Context;
import android.databinding.BindingAdapter;
import android.support.annotation.ArrayRes;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;

import timber.log.Timber;

/**
 * @author yawei
 * @data on 18-4-24  下午2:52
 * @project panowin
 * @org www.komlin.com
 * @email zyawei@live.com
 */
public class DataBindingAdapter {
/*
    @BindingAdapter("adapter")
    public static void visibleGone(RecyclerView view, RecyclerView.Adapter adapter) {
        view.setAdapter(adapter);
    }*/

    @BindingAdapter(value = {"textArray", "textLevel"})
    public static void setTextLevel(TextView textView, @ArrayRes int array, int level) {
        String[] arrays = textView.getContext().getResources().getStringArray(array);
        if (level < arrays.length && level >= 0) {
            textView.setText(arrays[level]);
        } else {
            textView.setText(arrays[0]);
        }
    }
    @BindingAdapter("android:visibility")
    public static void setVisibility(View view, Boolean value) {
        view.setVisibility(value ? View.VISIBLE : View.GONE);
    }
    @BindingAdapter("visibleGone")
    public static void visibleGone(View view, boolean visible) {
        view.setVisibility(visible ? View.VISIBLE : View.GONE);
    }

    @BindingAdapter("visibleInvisible")
    public static void visibleInvisible(View view, boolean visible) {
        view.setVisibility(visible ? View.VISIBLE : View.INVISIBLE);
    }

   /* @BindingAdapter("imageUrlRound")
    public static void setImageLevelRound(ImageView imageView, Data url) {
        Glide.with(imageView)
                .asBitmap()
                .load(url)
                .apply(new RequestOptions()
                        .transforms(new CenterCrop(), new RoundedCorners(DensityUtil.dip2px(imageView.getContext(), 4)))
                        .diskCacheStrategy(DiskCacheStrategy.RESOURCE)
                        .placeholder(R.drawable.ico_load)
                        .error(R.drawable.ico_lose))
                .into(imageView);
    }

    //添加一个占位
    @BindingAdapter("imageUrlCircle")
    public static void setImageUrlCircle(ImageView imageView, Data url) {
        Timber.i("url = " + url);
        Glide.with(imageView)
                .asBitmap()
                .load(url)
                .apply(new RequestOptions()
                        .transforms(new CenterCrop(), new RoundedCorners(1000))
                        .placeholder(R.drawable.ico_load)
                        .error(R.drawable.ico_lose))
                .into(imageView);
    }*/

    @BindingAdapter("imageRes")
    public static void setImageRes(ImageView imageView, int res) {
        Timber.i("url = " + res);
        Context context = imageView.getContext();
        Glide.with(context).load(res).into(imageView);
    }

  /*  @BindingAdapter("imageUrl")
    public static void setImageLevel(ImageView imageView, Data url) {
        Timber.i("url = " + url);
        Glide.with(imageView)
                .asBitmap()
                .load(url)
                .apply(new RequestOptions()
                        .diskCacheStrategy(DiskCacheStrategy.RESOURCE)
                        .placeholder(R.drawable.ico_load)
                        .error(R.drawable.ico_lose))
                .into(imageView);
    }

    @BindingAdapter("imageUrl")
    public static void setImageLevel(ImageView imageView, Uri url) {
        Timber.i("uri = " + url);
        Glide.with(imageView.getContext())
                .load(url)
                .apply(new RequestOptions().error(R.drawable.ico_lose))
                .into(imageView);
    }

*/
    @BindingAdapter("imageLevel")
    public static void setImageLevel(ImageView imageView, int level) {
        imageView.setImageLevel(level);
    }
}
